﻿package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Lvyouxianlu;
import com.server.LvyouxianluServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class LvyouxianluController {
	@Resource
	private LvyouxianluServer lvyouxianluService;


   
	@RequestMapping("addLvyouxianlu.do")
	public String addLvyouxianlu(HttpServletRequest request,Lvyouxianlu lvyouxianlu,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		lvyouxianlu.setAddtime(time.toString().substring(0, 19));
		lvyouxianluService.add(lvyouxianlu);
		session.setAttribute("backxx", "添加成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		
		//session.setAttribute("backurl", "lvyouxianluList.do");
		
		return "redirect:postback.jsp";
		//return "redirect:lvyouxianluList.do";
		
		
		
	}
 
//	处理编辑
	@RequestMapping("doUpdateLvyouxianlu.do")
	public String doUpdateLvyouxianlu(int id,ModelMap map,Lvyouxianlu lvyouxianlu){
		lvyouxianlu=lvyouxianluService.getById(id);
		map.put("lvyouxianlu", lvyouxianlu);
		return "lvyouxianlu_updt";
	}
	
	
	
	
	
//	后台详细
	@RequestMapping("lvyouxianluDetail.do")
	public String lvyouxianluDetail(int id,ModelMap map,Lvyouxianlu lvyouxianlu){
		lvyouxianlu=lvyouxianluService.getById(id);
		map.put("lvyouxianlu", lvyouxianlu);
		return "lvyouxianlu_detail";
	}
//	前台详细
	@RequestMapping("lyxlDetail.do")
	public String lyxlDetail(int id,ModelMap map,Lvyouxianlu lvyouxianlu){
		lvyouxianlu=lvyouxianluService.getById(id);
		map.put("lvyouxianlu", lvyouxianlu);
		return "lvyouxianludetail";
	}
//	
	@RequestMapping("updateLvyouxianlu.do")
	public String updateLvyouxianlu(int id,ModelMap map,Lvyouxianlu lvyouxianlu,HttpServletRequest request,HttpSession session){
		lvyouxianluService.update(lvyouxianlu);
		session.setAttribute("backxx", "修改成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		return "redirect:postback.jsp";
		//String url = request.getHeader("Referer");
		//return "redirect:"+url;
		//return "redirect:lvyouxianluList.do";
	}

//	分页查询   后台列表
	@RequestMapping("lvyouxianluList.do")
	public String lvyouxianluList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyouxianlu lvyouxianlu, String xianlubianhao, String xianlumingcheng, String mubiaojingdian, String chufachengshi, String chufashijian1,String chufashijian2, String xianlubaojia1,String xianlubaojia2, String xiangqingmiaoshu, String lvyougongsi, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 15);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 15);
		
		
		if(xianlubianhao==null||xianlubianhao.equals("")){pmap.put("xianlubianhao", null);}else{pmap.put("xianlubianhao", xianlubianhao);}		if(xianlumingcheng==null||xianlumingcheng.equals("")){pmap.put("xianlumingcheng", null);}else{pmap.put("xianlumingcheng", xianlumingcheng);}		if(mubiaojingdian==null||mubiaojingdian.equals("")){pmap.put("mubiaojingdian", null);}else{pmap.put("mubiaojingdian", mubiaojingdian);}		if(chufachengshi==null||chufachengshi.equals("")){pmap.put("chufachengshi", null);}else{pmap.put("chufachengshi", chufachengshi);}		if(chufashijian1==null||chufashijian1.equals("")){pmap.put("chufashijian1", null);}else{pmap.put("chufashijian1", chufashijian1);}		if(chufashijian2==null||chufashijian2.equals("")){pmap.put("chufashijian2", null);}else{pmap.put("chufashijian2", chufashijian2);}		if(xianlubaojia1==null||xianlubaojia1.equals("")){pmap.put("xianlubaojia1", null);}else{pmap.put("xianlubaojia1", xianlubaojia1);}		if(xianlubaojia2==null||xianlubaojia2.equals("")){pmap.put("xianlubaojia2", null);}else{pmap.put("xianlubaojia2", xianlubaojia2);}		if(xiangqingmiaoshu==null||xiangqingmiaoshu.equals("")){pmap.put("xiangqingmiaoshu", null);}else{pmap.put("xiangqingmiaoshu", xiangqingmiaoshu);}		if(lvyougongsi==null||lvyougongsi.equals("")){pmap.put("lvyougongsi", null);}else{pmap.put("lvyougongsi", lvyougongsi);}		
		int total=lvyouxianluService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyouxianlu> list=lvyouxianluService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyouxianlu_list";
	}
	
	@RequestMapping("lvyouxianluList2.do")
	public String lvyouxianluList2(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyouxianlu lvyouxianlu, String xianlubianhao, String xianlumingcheng, String mubiaojingdian, String chufachengshi, String chufashijian1,String chufashijian2, String xianlubaojia1,String xianlubaojia2, String xiangqingmiaoshu, String lvyougongsi, String issh,HttpServletRequest request){
		/*if(session.getAttribute("user")==null){
			return "login";
		}*/
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 15);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 15);
		
		pmap.put("lvyougongsi", (String)request.getSession().getAttribute("username"));
		if(xianlubianhao==null||xianlubianhao.equals("")){pmap.put("xianlubianhao", null);}else{pmap.put("xianlubianhao", xianlubianhao);}		if(xianlumingcheng==null||xianlumingcheng.equals("")){pmap.put("xianlumingcheng", null);}else{pmap.put("xianlumingcheng", xianlumingcheng);}		if(mubiaojingdian==null||mubiaojingdian.equals("")){pmap.put("mubiaojingdian", null);}else{pmap.put("mubiaojingdian", mubiaojingdian);}		if(chufachengshi==null||chufachengshi.equals("")){pmap.put("chufachengshi", null);}else{pmap.put("chufachengshi", chufachengshi);}		if(chufashijian1==null||chufashijian1.equals("")){pmap.put("chufashijian1", null);}else{pmap.put("chufashijian1", chufashijian1);}		if(chufashijian2==null||chufashijian2.equals("")){pmap.put("chufashijian2", null);}else{pmap.put("chufashijian2", chufashijian2);}		if(xianlubaojia1==null||xianlubaojia1.equals("")){pmap.put("xianlubaojia1", null);}else{pmap.put("xianlubaojia1", xianlubaojia1);}		if(xianlubaojia2==null||xianlubaojia2.equals("")){pmap.put("xianlubaojia2", null);}else{pmap.put("xianlubaojia2", xianlubaojia2);}		if(xiangqingmiaoshu==null||xiangqingmiaoshu.equals("")){pmap.put("xiangqingmiaoshu", null);}else{pmap.put("xiangqingmiaoshu", xiangqingmiaoshu);}		
		
		int total=lvyouxianluService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyouxianlu> list=lvyouxianluService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyouxianlu_list2";
	}	
	//前台列表
	@RequestMapping("lyxlList.do")
	public String lyxlList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyouxianlu lvyouxianlu, String xianlubianhao, String xianlumingcheng, String mubiaojingdian, String chufachengshi, String chufashijian1,String chufashijian2, String xianlubaojia1,String xianlubaojia2, String xiangqingmiaoshu, String lvyougongsi, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 15);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 15);
		
		
		if(xianlubianhao==null||xianlubianhao.equals("")){pmap.put("xianlubianhao", null);}else{pmap.put("xianlubianhao", xianlubianhao);}		if(xianlumingcheng==null||xianlumingcheng.equals("")){pmap.put("xianlumingcheng", null);}else{pmap.put("xianlumingcheng", xianlumingcheng);}		if(mubiaojingdian==null||mubiaojingdian.equals("")){pmap.put("mubiaojingdian", null);}else{pmap.put("mubiaojingdian", mubiaojingdian);}		if(chufachengshi==null||chufachengshi.equals("")){pmap.put("chufachengshi", null);}else{pmap.put("chufachengshi", chufachengshi);}		if(chufashijian1==null||chufashijian1.equals("")){pmap.put("chufashijian1", null);}else{pmap.put("chufashijian1", chufashijian1);}		if(chufashijian2==null||chufashijian2.equals("")){pmap.put("chufashijian2", null);}else{pmap.put("chufashijian2", chufashijian2);}		if(xianlubaojia1==null||xianlubaojia1.equals("")){pmap.put("xianlubaojia1", null);}else{pmap.put("xianlubaojia1", xianlubaojia1);}		if(xianlubaojia2==null||xianlubaojia2.equals("")){pmap.put("xianlubaojia2", null);}else{pmap.put("xianlubaojia2", xianlubaojia2);}		if(xiangqingmiaoshu==null||xiangqingmiaoshu.equals("")){pmap.put("xiangqingmiaoshu", null);}else{pmap.put("xiangqingmiaoshu", xiangqingmiaoshu);}		if(lvyougongsi==null||lvyougongsi.equals("")){pmap.put("lvyougongsi", null);}else{pmap.put("lvyougongsi", lvyougongsi);}		
		int total=lvyouxianluService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyouxianlu> list=lvyouxianluService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyouxianlulist";
	}
	
	@RequestMapping("deleteLvyouxianlu.do")
	public String deleteLvyouxianlu(int id,HttpServletRequest request){
		lvyouxianluService.delete(id);
		String url = request.getHeader("Referer");
		return "redirect:"+url;
		//return "redirect:lvyouxianluList.do";
	}
	
	
}
